<!-- 楼盘设置驻守人 -->
<template>
    <div class="modal fade" id="settingDefendModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="z-index: 1000">
        <div class="modal-dialog" style="width: 1000px;">
            <div class="modal-content" style="text-align: center;width: 1000px;">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">{{referrerTitle}}</h4>
                </div>
                <div class="modal-body" >
                    <setting-referrer :premises-id="premisesId" ref="settingReferrer"
                                      :recommend-list="recommendList"
                                      :loading="getReferrerLoading"
                    ></setting-referrer>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn checkedBtn" @click="saveRecommend">保存设置</button>
                </div>
            </div>
        </div>
        <premisesListModal @getPremisesListener='getPremisesReturn'></premisesListModal>
    </div>
    
</template>

<script>
import { Modal } from 'iview'
import {API, Log, Utils,  } from '../../../js/base'
import SettingReferrer from './SettingReferrer.vue'
import premisesListModal from './PremisesListModal.vue'
export default {
  name: 'ReferrerModal',
  props: {
    referrerTitle: {
      type: String,
      default: ''
    },
    // loading: {
    //   type: Boolean,
    //   default: true,
    // },
    premisesId: {
      type: String,
      default: null
    }
  },
  components: {
    Modal,
    SettingReferrer,
    premisesListModal
  },
  watch: {
    premisesId (val) {
      if (!val) return
      this.getRecommendList(val)
    }
  },
  data () {
    return {
      findPremisesList: [],
      selectPremises: [],
      choosePremisesVisible: false,
      getReferrerLoading: false,
      recommendList: [],
      allPremises: {
        premisesName: '全部',
        premisesId: 'ALL',
        premisesIdList: []
      }
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    openPremisesList () {
      // 打开选择楼盘弹窗, 清空已选值
      this.selectPremises = [];
      $('#premisesListModal').modal({
        backdrop: 'static',
        keyboard: false
      })
    },
    init () {
      //this.findPremisesLists()
    },
    findPremisesLists () {
      const _this = this
      const url = this.utilHelper.apiUrl + '/api/getCaseMemberList'
      let body = {}
      body.params = {
        userId: window.localStorage.getItem('userId')
      }
      _this.$http.post(url, body).then((response) => {
        _this.findPremisesList = response.body.resData.caseMemberList
        if (_this.findPremisesList && _this.findPremisesList.length) {
          _this.findPremisesList.forEach(item => {
            _this.allPremises.premisesIdList.push(item.premisesId)
          })
        }
        _this.findPremisesList.unshift(_this.allPremises)
      })
    },
    saveRecommend () {
      const _this = this
      let recommendIdList = []
      this.$refs.settingReferrer.tableData.forEach(item => {
        recommendIdList.push(item.id || item.recommenderId)
      })
      const applyOtherFlag = _this.selectPremises.length !== 0
      // 保存推荐人
      const p1 = Service.Recommend.saveRecommend({
        premisesId: _this.premisesId,
        recommendIdList,
        _this
      }).then(rsp => {
        if (!applyOtherFlag) {
          _this.resetData()
        }
        return rsp
      }).catch(e => {
        if (!applyOtherFlag) {
          _this.resetData()
        }
        return e
      })
      // 若应用到其他楼盘没设置 直接返回
      if (!applyOtherFlag) return
      // 提出应用到其他楼盘中的主楼盘Id
      var index = _this.selectPremises.indexOf(_this.premisesId);
      if(index >= 0){
        _this.selectPremises.splice(index, 1);
      }
      const p2 = Service.Recommend.batchSyncRecommend({
        premisesIdList: _this.selectPremises,
        recommendIdList,
        _this
      }).then(rsp => {
        return rsp
      }).catch(e => {
        return e
      })
      Promise.all([p1, p2]).then(rsp => {
        _this.resetData()
      }).catch(e => {
        _this.resetData()
      })
    },
    /**
     * 关闭弹框，重置数据
     */
    resetData () {
      // this.premisesId = []
      this.selectPremises = []
      this.$emit("setReferrerListener");
      $('#settingDefendModal').modal('hide')
    },
    /**
     * 获取推荐人列表
     */
    getPartnerByPremisesList (id = this.premisesId) {
      const _this = this
      _this.getReferrerLoading = true
      _this.recommendList = []
      //const tmpList = []
      if(!id) return
      let index = -1
      Service.Premise.getPartnerByPremisesList(id, this).then(list => {
        _this.getReferrerLoading = false
        if (list && list.length) {
            debugger;
          list.forEach(item => {
            item.dempName = item.deptName
            item.accountName = item.userName
              index = _this.recommendList.findIndex(i => i.recommenderUuid === item.recommenderUuid)
              if(index < 0) { // 未找到
                  _this.recommendList.push(item)
              }
          })
          //_this.recommendList = list
        }
      }).catch(e => {
        _this.getReferrerLoading = false
      })
    },
    getPremisesReturn(msg){
      var _this = this;
      _this.selectPremises = msg;
    }
  }
}
</script>

<style scoped>
/*    .action-button{font-size:32px;color:#f87242;}
    .col{text-align: left;}
    .modelList{margin-left: 20px;width: 400px;}*/
    /*.modelList li{width: 399px;height:35px;line-height: 30px;border-bottom: 1px dashed #bbbbbb}*/
    /*.modelList li label{text-align: left;}*/
    /*.modelList li icon{text-align: right;}*/
    /*.text-column{width: 120px;height: 20px;}*/
    /*.model-left{width: 430px;height:570px;float: left;border-right:1px solid #bbbbbb;position: relative}*/
    /*.model-left-tip{position: absolute;bottom: 5px;left:5px;}*/
    /*.model-right{width: 510px;height:570px;float: left;padding: 0 15px;text-align: left;}*/
    /*.model-right h3{margin-bottom: 20px;}*/
    /*.model-set{width: 510px;height: 380px;}*/
    /*.model-set h3{height: 24px;}*/

    /*.model-set-info h3{text-align: left}*/
</style>
